home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-api-22.lha / AmiTCP-2.2 / src / netlib / getpasswdent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-22  |  3.2 KB  |  166 lines

  1. RCS_ID_C "$Id: getpasswdent.c,v 1.3 1993/10/15 01:16:22 ppessi Exp $";
  2. /*
  3.  * getpasswdent.c - getpwd*() for network library
  4.  *
  5.  * This file is part of the AmiTCP/IP Network Library.
  6.  *
  7.  * Author: ppessi <Pekka.Pessi@hut.fi>
  8.  * Copyright © 1993 AmiTCP/IP Group, <AmiTCP@snakemail.hut.fi>
  9.  *                  Helsinki University of Technology, Finland.
  10.  *
  11.  * Created      : Sun Jun 20 17:52:37 1993 ppessi
  12.  * Last modified: Thu Oct 14 19:24:28 1993 ppessi
  13.  */
  14.  
  15. #include <exec/memory.h>
  16. #include <dos/dos.h>
  17.  
  18. #include <string.h>
  19. #include <sys/errno.h>
  20.  
  21. #include "pwd.h"
  22.  
  23. #if !NETLIB            /* is this static or */
  24. #error "Shared Library features are unimplemented"
  25. #else
  26. #include <proto/dos.h>
  27.  
  28. #include <errno.h>
  29. #define SetNetErr(x) (errno=(x))
  30.  
  31. static BPTR pwdfh = NULL;
  32.  
  33. #define INTERNAL static
  34.  
  35. #endif
  36.  
  37. /*
  38.  * Parsing function
  39.  */
  40. int do_parse(UBYTE *str, int n, UBYTE **array);
  41.  
  42. /* End reading passwd entities */
  43. INTERNAL void EndPasswdEnt(void)
  44. {
  45.   if (pwdfh) Close(pwdfh), pwdfh = NULL;
  46. }
  47.  
  48. /* Restart reading passwd entities */
  49. INTERNAL void SetPasswdEnt(void)
  50. {
  51.   if (pwdfh) EndPasswdEnt();
  52.   
  53.   if (!(pwdfh = Open(_PATH_PASSWD, MODE_OLDFILE))) 
  54.     return (void)SetNetErr(ENOENT);
  55. #if 0
  56.   atexit(EndPasswdEnt);
  57. #endif
  58. }
  59.  
  60. /* Read a next entry from the passwd-file database */
  61. INTERNAL struct passwd *GetPasswd(struct passwd *pwd, ULONG size)
  62. {
  63.   UBYTE *buffer = (UBYTE *)pwd;
  64.   int err = 0;
  65.  
  66.   if (size - sizeof(struct passwd) < 0) {
  67.     err = ENOBUFS;
  68.     goto bad;
  69.   }
  70.  
  71.   memset(buffer, sizeof(struct passwd), 0);
  72.   size   -= sizeof(struct passwd) + 1; 
  73.   buffer += sizeof(struct passwd);
  74.   /* fix a bug in the FGetS routine */
  75.   buffer[size] = '\0';
  76.  
  77.   /* Open file? */
  78.   if (!pwdfh) 
  79.     SetPasswdEnt();
  80.  
  81.   if (!pwdfh || !FGets(pwdfh, buffer, size)) {
  82.     err = EIO;
  83.     goto bad;
  84.   }
  85.  
  86.   if (err = do_parse(buffer, 7, (UBYTE **)pwd))
  87.     goto bad;
  88.  
  89.   /* Parse the UID field */
  90.   if (StrToLong((UBYTE*)pwd->pw_uid, &pwd->pw_uid) <= 0 ||
  91.       /* Parse the GID field */
  92.       StrToLong((UBYTE*)pwd->pw_gid, &pwd->pw_gid) <= 0) {
  93.     err = EFTYPE;
  94.     goto bad;
  95.   }
  96.  
  97.   return pwd;
  98.  
  99.  bad:
  100.   SetNetErr(err);
  101.   EndPasswdEnt();
  102.   return (struct passwd*)NULL;
  103. }
  104.  
  105. /* Search for an entry with a matching user ID */
  106. INTERNAL struct passwd *GetPasswdUID(ULONG uid, 
  107.                 struct passwd *p, 
  108.                 ULONG size)
  109. {
  110.   while ((p = GetPasswd(p, size)) && (p->pw_uid != uid))
  111.     ;
  112.   EndPasswdEnt();
  113.   return p;
  114. }
  115.  
  116. /* Search for an entry with a matching login name */
  117. INTERNAL struct passwd *GetPasswdName(const UBYTE *name, 
  118.                  struct passwd *p, 
  119.                  ULONG size)
  120. {
  121.   while ((p = GetPasswd(p, size)) && strcmp(p->pw_name, name))
  122.     ;
  123.   EndPasswdEnt();
  124.   return p;
  125. }
  126.  
  127. #if NETLIB
  128.  
  129. #include <stdlib.h>
  130.  
  131. /*
  132.  * A global structure for POSIX compatible functions
  133.  */
  134. #define PSTORE_SIZE 256
  135. static struct passwd * pstore = NULL;
  136.  
  137. static struct passwd * alloc_pstore(void)
  138. {
  139.   return pstore = (struct passwd *)malloc(PSTORE_SIZE);
  140. }
  141. #endif
  142.  
  143. /* 
  144.  * Unix compatible getgrgid()
  145.  */
  146. struct passwd *getpwuid(uid_t uid)
  147. {
  148.   if (!pstore && !alloc_pstore()) {
  149.     return NULL;
  150.   }
  151.  
  152.   return GetPasswdUID((ULONG) uid, pstore, PSTORE_SIZE);
  153. }
  154.  
  155. /*
  156.  * Unix compatible getpwnam
  157.  */
  158. struct passwd *getpwnam(const char *name)
  159. {
  160.   if (!pstore && !alloc_pstore()) {
  161.     return NULL;
  162.   }
  163.   return GetPasswdName((UBYTE *) name, pstore, PSTORE_SIZE);
  164. }
  165.  
  166.